<?php

include_once('admin.php');

/**
 * The Location Management Class provides functions to add, remove or modify locations (Requires Super User access)
 * @package admin
 */
class location_management extends admin
{
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		
		// Redirect if not logged in as admin
		if ( !$this->session->userdata('CurrentUser') || !$this->session->userdata('CurrentUser')->su_access )
			redirect('authenticate/login');
		
		// Load the form helper
		$this->load->helper('form');
		
		// Load the form validation library
		$this->load->library('form_validation');
		
		// Load models
		$this->load->model('location_model');
    }
	
	/**
     * Displays a list of all locations
     */
	public function index( )
	{
		// Get contents of location Table
		$this->data['locations'] = $this->location_model->getAllLocations( );
	
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('admin/location_management/search', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	/**
     * Delete a location
	 * @param $location_id Location ID of location to delete
     */
	public function delete( $location_id )
	{
		// Delete user
		$this->location_model->deleteLocation( $location_id );
	
		// Redirect back to previous search
		redirect( 'admin/location_management/' );
	}
	
	/**
     * Modify a location
	 * @param $location_id Location ID of location to modify
     */
	public function modify( $location_id )
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('admin/location_management/modify'))
			{
				// Get the data to insert
				$form_data = array(	'location_id' => $this->input->post('location_id'),
							'location_name' => $this->input->post('location_name'));
			
				// Update volumes where access_id = $access_id with the values retrieved from the form
				$this->location_model->updateLocation( $location_id, $form_data );
			
				redirect('admin/location_management/');
			}
		}
	
		// Get user details
		$this->data['location'] = $this->location_model->getLocation($location_id)->row();
	
		// Load the "add" view
		$this->data['main_content'] = $this->load->view('admin/location_management/modify_location', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);	
	}

	/**
     * Add a location
     */
	public function add( )
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('admin/location_management/add'))
			{
				// Get the data to insert
				$form_data = array(	'location_id' => $this->input->post('location_id'),
							'location_name' => $this->input->post('location_name'));
			
				// Update volumes where access_id = $access_id with the values retrieved from the form
				$this->location_model->addLocation( $form_data );
			
				redirect('admin/location_management/');
			}
		}
	
		// Load the "add" view
		$this->data['main_content'] = $this->load->view('admin/location_management/add_location', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
}

?>